package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Apptype;
import com.ruoyi.system.domain.Buildinfo;
import com.ruoyi.system.service.IBuildinfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Floor;
import com.ruoyi.system.service.IFloorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼层信息Controller
 * 
 * @author yaozh
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/system/floor")
public class FloorController extends BaseController
{
    @Autowired
    private IFloorService floorService;

    @Autowired
    private IBuildinfoService buildinfoService;

    /**
     * 查询楼层信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:floor:list')")
    @GetMapping("/list")
    public TableDataInfo list(Floor floor,@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        if(appIds.length > 0){

        }else{
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Floor> list = floorService.selectFloorList(appIds,floor, dbName);
        return getDataTable(list);
    }

    /**
     * 导出楼层信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:floor:export')")
    @Log(title = "楼层信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Floor floor,@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        List<Floor> list = new ArrayList<>();
        if(appIds.length > 0){
            list = floorService.selectFloorList(appIds,floor, dbName);
        }
        ExcelUtil<Floor> util = new ExcelUtil<Floor>(Floor.class);
        util.exportExcel(response, list, "楼层信息数据");
    }

    /**
     * 获取楼层信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:floor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,@RequestParam String dbName)
    {
        return success(floorService.selectFloorById(id, dbName));
    }

    /**
     * 新增楼层信息
     */
    @PreAuthorize("@ss.hasPermi('system:floor:add')")
    @Log(title = "楼层信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Floor floor,@RequestParam String dbName)
    {
        return toAjax(floorService.insertFloor(floor, dbName));
    }

    /**
     * 修改楼层信息
     */
    @PreAuthorize("@ss.hasPermi('system:floor:edit')")
    @Log(title = "楼层信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Floor floor,@RequestParam String dbName)
    {
        return toAjax(floorService.updateFloor(floor, dbName));
    }

    /**
     * 删除楼层信息
     */
    @PreAuthorize("@ss.hasPermi('system:floor:remove')")
    @Log(title = "楼层信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,@RequestParam String dbName)
    {
        return toAjax(floorService.deleteFloorByIds(ids, dbName));
    }

    /**
     * 查询楼栋信息列表
     */
    @GetMapping("/build")
    public AjaxResult listAllBuild(@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        if(appIds.length > 0){

        }else{
            return success(new ArrayList<>());
        }
        List<Buildinfo> list = buildinfoService.selectBuildinfoList(appIds,new Buildinfo(),dbName);
        return success(list);
    }
}
